package com.pucheng.bracelet.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.pucheng.bracelet.app.util.DatetimeUtil;
import com.pucheng.bracelet.data.bean.AllHertRateBean;
import com.pucheng.bracelet.data.bean.HertRateNode;

import java.util.ArrayList;

/**
 * 心率折线图
 */
public class HeartrateMapView extends LineChart {
    public HeartrateMapView(Context context) {
        super(context);
        init();
    }

    public HeartrateMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HeartrateMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    @Override
    protected void init() {
        super.init();
        getDescription().setEnabled(false);
        setPinchZoom(false);
        setScaleEnabled(false);
        setDrawGridBackground(false);
        setTouchEnabled(false);
        getLegend().setEnabled(false);

        setBackgroundColor(getResources().getColor(android.R.color.transparent));

        XAxis xAxis = getXAxis();
        xAxis.setLabelCount(7);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.WHITE);
        xAxis.enableGridDashedLine(10f, 10f, 0f);

        YAxis leftAxis = getAxisLeft();
        leftAxis.setTextColor(Color.WHITE);
        leftAxis.setEnabled(false);
        leftAxis.setDrawAxisLine(true);
        leftAxis.setDrawGridLines(true);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(200f);
        leftAxis.setDrawZeroLine(true);
        leftAxis.setZeroLineWidth(1f);
        leftAxis.setZeroLineColor(Color.WHITE);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);

        YAxis rightAxis = getAxisRight();
        rightAxis.setEnabled(false);
    }

    public void setData(AllHertRateBean bean) {


        ArrayList<Entry> values = new ArrayList<>();

        String startTime[] = DatetimeUtil.formatDate(bean.getTimestamp(), DatetimeUtil.DATE_PATTERN_HHMM).split("[:]");
        int start = Integer.parseInt(startTime[0]) * 60 + Integer.parseInt(startTime[1]);
        for (float x = 0; x <= 24 * 60; x++) {
            if (x >= start) {
                for (HertRateNode node : bean.getNodes()) {
                    if (node !=null){
                        if (x==node.getMin_offset()+start) {
                            values.add(new Entry(x, node.getBpm()));
                        }
                    }
                }
            }else {
                values.add(new Entry(x, 0));
            }
        }

        // create a dataset and give it a type
        LineDataSet set1 = new LineDataSet(values, "DataSet 1");
//        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.setColor(Color.WHITE);
        set1.setValueTextColor(Color.WHITE);
        set1.setLineWidth(2f);
        set1.setDrawCircles(false);
//        set1.setDrawValues(false);
//        set1.setFillAlpha(65);
//        set1.setFillColor(Color.WHITE);
        set1.setHighLightColor(Color.WHITE);
//        set1.setDrawCircleHole(false);

        // create a data object with the data sets
        LineData data = new LineData(set1);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(9f);

        // set data
        setData(data);
    }

    float getRandom(float range, float start) {
        return (float) (Math.random() * range) + start;
    }
}
